package pl.edu.agh.portal.application.portlets;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.task.client.TaskManagerStub;
import pl.edu.agh.thesis.webservices.task.client.TaskUtils;

public class TaskDetailsPortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/task_details_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		
		TaskManagerStub.Task task = TaskUtils.get(PortletsUtils.temp_session, Integer.parseInt((String)PortletsUtils.vault.get("taskId")) );
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_FOR_DETAILS, task);
		
		//TODO		
		//CommentManagerStub.Comment[] comments = CommentUtils.getAll(String.valueOf(task.getId()), CommentManagerSkeleton.TASK_ID_RESTRICTION);
		//request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_DETAILS_COMMENTS, comments);
		
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {		
		String cancel = request.getParameter(PortletsUtils.BUTTON_CANCEL);
		String edit = request.getParameter(PortletsUtils.BUTTON_EDIT);
				
		if (cancel != null) {			
			response.sendRedirect("/portal/portal/project_details.psml");
		} else if (edit != null) {
			response.sendRedirect("/portal/portal/task_edit.psml");
		} else {
			response.setPortletMode(PortletMode.VIEW);
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);		
	}
		
	public void destroy() {
		normalView = null;
		super.destroy();
	}
	
}
